#ifndef __NETLIB_H__
#define __NETLIB_H__

/**
*@file       netlib.h
*@brief      
*
*@author     graywind
*@version    1.0.0.0
*@data       2011/12/03
*/


#include "common.h"

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///@brief   service relative


///@brief   create service
///retval   void
extern _NET_LIB void __stdcall create_service();
///@brief   destroy service
///retval   void
extern _NET_LIB void __stdcall destroy_service();
///@brief   whether initialize service
extern _NET_LIB bool __stdcall is_service_init();
///@breif   initialize service
///param[in]   thread_num    how many threads be used
///param[in]   max_sessio    max number of sessions
///param[in]   buffer_length bytes length of the buffer used in the session
extern _NET_LIB void __stdcall initialize_service(int thread_num, int max_session, long buffer_length);
///@brief   start the service
///retval   void
extern _NET_LIB void __stdcall start_service();
///@brief   shut down the service
///retval   void
extern _NET_LIB void __stdcall terminate_service();


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///@brief   relative call

///@brief   session handle
typedef  int   pnSessionHandle;
///@brief   value of invalid handle
const int      INVALID_SESSION = -1;

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///@brief   start acceptor
///param[in]   port     port to be used with this server
///param[in]   msgProc  ptr of callback function
///retval   bool
extern _NET_LIB  bool __stdcall start_listen(unsigned short port, MsgProc msgProc);
///@brief   create a connection
///param[in]  ip            ip address of the server
///param[in]  port          port of the server
///param[in]  msgProc       ptr of callback function
///retval   int(pnSessionHandle)
extern _NET_LIB pnSessionHandle __stdcall start_connect(const char* ip, unsigned short port, MsgProc msgProc);
///@brief   disable acceptor
///retval   void
extern _NET_LIB void __stdcall close_listen();
///@brief   close all session
///retval   void
extern _NET_LIB void __stdcall close_all_session();
///@brief   close a session
///param[in] session_handle    identification of the session
///retval   void
extern _NET_LIB void __stdcall close_session(pnSessionHandle session_handle);
///@brief   broadcast a message to all live sessions
///param[in]  msg_id      message identification
///param[in]  msg_len     bytes length of message
///param[in]  msg_buffer  point to the content
///param[in]  type        session type
///retval   int
extern _NET_LIB int __stdcall broadcast_msg(unsigned long msg_id, unsigned long msg_len, void* msg_buffer, eUsageType type);
///@brief   send a message to a session
///param[in]  session_handle  identification of the session
///param[in]  msg_id          message identification
///param[in]  msg_len         bytes length of message
///param[in]  msg_buffer      point to the content 
///retval   bool
extern _NET_LIB bool __stdcall send_msg(pnSessionHandle session_handle, unsigned long msg_id, unsigned long msg_len, void* msg_buffer);
///@brief   max count of connection
///retval   int
extern _NET_LIB int  __stdcall max_session();
///@brief   how many clients connected with this server
///retval   int
extern _NET_LIB int  __stdcall cur_listen();
///@brief   how many server connected with this client
///retval   int
extern _NET_LIB int         __stdcall cur_connect();
///@brief   whether this session is alive
///param[in]  session_index  identification of the session
///retval   bool
extern _NET_LIB bool __stdcall is_alive(int session_handle);
///@brief   get usage type
///param[in]  session_handle    identification of the session
///retval   eUsageType
extern _NET_LIB eUsageType __stdcall get_usage(int session_handle);

#endif
